<!-- 记住密码 -->
<html>
<head>
    <meta charset="UTF-8">
</head>
<script src="//cdn.66173.cn/www/js/jquery-1.8.3.min.js"></script>
<body onLoad="checkCookie()">
<p>
    <input type="text" name="username" id="user" placeholder="请输入用户名">
</p>
<p>
    <input type="password" name="password1" id="pwd" placeholder="请输入密码">
</p>
<input type="checkbox" id="check"  value=""><span>记住我</span>
<p><input type="button" value="确定" onclick="checkCookie()"></p>
<p>
    <input type="button" value="清除cookie" onclick="clearAllCookie()">
</p>
</body>
<script type="text/javascript">
    <!--清除cookie-->
    function clearAllCookie() {
        var keys = document.cookie.match(/[^ =;]+(?=\=)/g);
        if(keys) {
            for(var i = keys.length; i--;)
                document.cookie = keys[i] + '=0;expires=' + new Date(0).toUTCString()
        }
    }
    function getCookie(c_name) //根据分隔符每个变量的值
    {
        if (document.cookie.length > 0) {
            c_start = document.cookie.indexOf(c_name + "=")
            if (c_start != -1) {
                c_start = c_start + c_name.length + 1;
                c_end = document.cookie.indexOf("^",c_start);
                if (c_end==-1) c_end=document.cookie.length;
                return unescape(document.cookie.substring(c_start,c_end));
            }
        }
        return "";
    }
    function setCookie(c_name, n_value, p_name, p_value, expiredays) //设置cookie
    {
        var exdate = new Date();
        exdate.setDate(exdate.getDate() + expiredays);
        document.cookie = c_name + "=" + escape(n_value) + "^" + p_name + "=" + escape(p_value) + ((expiredays == null) ? "" : "^;expires=" + exdate.toGMTString());
        console.log(document.cookie)
    }

    function checkCookie() //检测cookie是否存在，如果存在则直接读取，否则创建新的cookie
    {
        console.log(document.cookie)
        var username = getCookie('username');
        var password = getCookie('password');
        $("#user").attr('placeholder',username)
        $('#pwd').attr('placeholder',password)
        $("#user").attr('value',username)
        $("#pwd").attr('value',password)
        $("#check").attr("checked","checked")
        if (username != null && username != "" && password != null && password != "") {
            username= $('#user').val();
            password=$('#pwd').val();
        }
        else {
            username = $('#user').val()
            password = $('#pwd').val()
            if (username != null && username != "" && password != null && password != "")
            {
                setCookie('username', username, 'password', password, 365);
                $("#user").attr('placeholder',username)
                $('#pwd').attr('placeholder',password)
                $("#user").attr('value',username)
                $("#pwd").attr('value',password)
                $("#check").attr("checked","checked")
            }
        }
        console.log(document.cookie)
    }
    function cleanCookie (c_name, p_name) { //使cookie过期
        document.cookie = c_name + "=" + ";" + p_name + "=" + ";expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
</script>
</html>
